package com.chinasoft.controller;

import com.chinasoft.domain.Book;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class AccountController {
    @RequestMapping("/showData1")
    public void showData1(HttpServletResponse response) throws IOException {
        response.getWriter().print("message");
    }

    //@ResponseBody:将返回结果作为页面内容
    @RequestMapping("/showData2")
    @ResponseBody
    public String showData2()  {
        return "{'name':'jack'}";
    }

    @RequestMapping("/showData3")
    @ResponseBody
    public String showData3() throws JsonProcessingException {
        Book book = new Book();
        book.setName("springtest");
        book.setPrice(66.0);
        ObjectMapper om = new ObjectMapper();

        return om.writeValueAsString(book);
    }

    @RequestMapping("/showData4") //配合 mvc:annotation-driven
    @ResponseBody
    public Book showData4() throws JsonProcessingException {
        Book book = new Book();
        book.setName("springtest");
        book.setPrice(66.0);
        return book;
    }

    @RequestMapping("/showData5") //配合 mvc:annotation-driven
    @ResponseBody
    public List showData5() throws JsonProcessingException {
        Book book1 = new Book();
        book1.setName("springtest1");
        book1.setPrice(66.0);
        Book book2 = new Book();
        book2.setName("springtest2");
        book2.setPrice(76.0);
        ArrayList a1 = new ArrayList();
        a1.add(book1);
        a1.add(book2);
        return a1;
    }


}